package co.yixiang.modules.shop.rest.Arest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.shop.domain.YxStoreCart;
import co.yixiang.modules.shop.domain.YxStoreProduct;
import co.yixiang.modules.shop.domain.YxStoreProductAttrResult;
import co.yixiang.modules.shop.service.YxStoreProductAttrResultService;
import co.yixiang.modules.shop.service.YxStoreProductService;
import co.yixiang.modules.shop.service.dto.YxStoreProductDto;
import co.yixiang.modules.shop.service.dto.YxStoreProductQueryCriteria;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Api(tags = "Api2:商城:商品管理")
@RestController
@RequestMapping("api2/yxStoreProduct")

public class AStoreProductController extends ABaseController  {

    @Autowired
    public YxStoreProductService yxStoreProductService;
    //sku列表
    @Autowired
    public YxStoreProductAttrResultService productAttrResultService;

    @ApiOperation(value = "立刻购买")
    @PostMapping("/buy")
    public ResponseEntity buy(@Validated @RequestBody YxStoreCart cart ){
        // TODO: 2021/4/27
        // yxStoreCartService.save(cart);
        return ResponseEntity.ok(cart);
    }

    @ApiOperation(value = "分页查询商品")
    @GetMapping(value = "/")
    public ResponseEntity getYxStoreProducts(YxStoreProductQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(yxStoreProductService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation(value = "查询商品详情")
    @GetMapping(value = "/{id}")
    public YxStoreProductDto getYxStoreProduct(
            @PathVariable @ApiParam(value = "商品id",required = true) Integer id) {
        YxStoreProductDto one = yxStoreProductService.getOne(id);
        one.setSliderImageList(Arrays.asList(one.getSliderImage().split(",").clone()));
        return one;
    }


    @ApiOperation(value = "查询商品属性")
    @GetMapping(value = "/attr/{id}")
    public ResponseEntity getAttr(
            @PathVariable @ApiParam(value = "商品id",required = true) Integer id){
        YxStoreProductAttrResult attr = productAttrResultService.getOne(
                new LambdaQueryWrapper<YxStoreProductAttrResult>()
                        .eq(YxStoreProductAttrResult::getProductId, id));
        return new ResponseEntity(attr, HttpStatus.OK);
    }

}
